package com.aier.user.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.user.entity.FeedbackContent;

public class FeedbackContentRowMapper implements RowMapper<FeedbackContent> {

    @SuppressWarnings("unused")
    private int type;

    public FeedbackContentRowMapper() {
    }

    public FeedbackContentRowMapper(int type) {
        this.type = type;
    }

    @Override
    public FeedbackContent mapRow(ResultSet rs, int rowNum) throws SQLException {
        return parseFeedbackContent(new FeedbackContent(), rs);
    }

    public static FeedbackContent parseFeedbackContent(FeedbackContent f, ResultSet rs)
        throws SQLException {
        return parseFeedbackContent(f, rs, null);
    }

    public static FeedbackContent parseFeedbackContent(FeedbackContent f, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            f.setFbid(rs.getLong(su + "fbid"));
        } catch (SQLException e) {
            f.setFbid(null);
        }
        try {
            f.setUid(rs.getLong(su + "uid"));
        } catch (SQLException e) {
            f.setUid(null);
        }
        try {
            String str = rs.getString(su+"content");
            if (str != null) {
                str = str.replaceAll("<", "&lt;");
                str = str.replaceAll(">", "&gt;");
            }
            f.setContent(str);
        } catch (SQLException e) {
            f.setContent(null);
        }
        try {
            f.setCdate(rs.getTimestamp(su + "cdate"));
        } catch (SQLException e) {
            f.setCdate(null);
        }
        try {
            f.setName(rs.getString(su + "name"));
        } catch (SQLException e) {
            f.setName(null);
        }
        try {
            f.setAccount(rs.getString(su + "account"));
        } catch (SQLException e) {
            f.setAccount(null);
        }
        return f;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, FeedbackContent f) {
        par.put("fbid", f.getFbid());
        par.put("uid", f.getUid());
        par.put("content", f.getContent());
        par.put("cdate", f.getCdate());
        par.put("name", f.getName());
        par.put("account", f.getAccount());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args,
            FeedbackContent f) {
        args.put("fbid", f.getFbid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final FeedbackContent f) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(f),
                        new String[] { "fbid" });
                parsePS(ps, f);
                return ps;
            }

        };
    }

    private static String parseSQL(FeedbackContent f) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into feedback_content(");
        int count = 0;
        if (null != f.getFbid()) {
            sql.append("fbid,");
            count++;
        }
        if (null != f.getUid()) {
            sql.append("uid,");
            count++;
        }

        if (null != f.getContent()) {
            sql.append("content,");
            count++;
        }

        if (null != f.getCdate()) {
            sql.append("cdate,");
            count++;
        }
        
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, FeedbackContent f)
        throws SQLException {
        int count = 1;
        if (null != f.getFbid()) {
            ps.setLong(count++, f.getFbid());
        }

        if (null != f.getUid()) {
            ps.setLong(count++, f.getUid());
        }
        
        if (null != f.getContent()) {
            ps.setString(count++, f.getContent());
        }

        if (null != f.getCdate()) {
            ps.setTimestamp(count++, f.getCdate());
        }

    }

}
